<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.Map.Entry,java.util.*,java.util.regex.*,java.net.*,java.io.*,java.text.*"%>
<%@ page import="com.tianxiabuyi.*,com.tianxiabuyi.db.*,com.tianxiabuyi.model.*,com.tianxiabuyi.util.*"%>
<%@ page import="org.jsoup.Jsoup,org.jsoup.nodes.Element,org.jsoup.select.Elements"%>
<%@ page import="com.google.gson.reflect.TypeToken"%>
<%@ page import="org.dom4j.*"%>
<%@ page import="org.hibernate.StatelessSession"%>
<%@ page import="org.hibernate.criterion.CriteriaSpecification"%>
<%@ page import="org.hibernate.SQLQuery"%>
<%@ page import="java.text.DecimalFormat"%>
<%@ page import="java.sql.*"%>
<%!
	static final Integer EXPIRES_IN = 3600; // token有效时间(秒)

	// 问卷题型
	static final Integer SURVEY_QUESTION_TYPE_PARENT = 0; // 副标题
	static final Integer SURVEY_QUESTION_TYPE_SINGLE = 1; // 单选题
	static final Integer SURVEY_QUESTION_TYPE_MULTI = 2; // 多选题
	static final Integer SURVEY_QUESTION_TYPE_INPUT = 3; // 填空题
	static final Integer SURVEY_QUESTION_TYPE_GRADE = 4; // 打分题
	static final Integer SURVEY_QUESTION_TYPE_MUTILINPUT = 5; // 多重填空题
	static final Integer SURVEY_QUESTION_TYPE_MUTILINPUT_CHILD = 6; // 多重填空题子题目
	// 问卷分类
	static final Integer SURVEY_CATEGORY_COMMON = 1; // 模板
	static final Integer SURVEY_CATEGORY_FOLLOW = 2; // 复诊随访
	static final Integer SURVEY_CATEGORY_QUESTION = 3; // 问卷随访
	static final Integer SURVEY_CATEGORY_GRADE = 4; // 满意度调查

	// 时间轴类型
	static final Integer TIME_LINE_OPERATE = 1; // 手术类型
	static final Integer TIME_LINE_FOLLOW = 2; // 复诊随访类型
	static final Integer TIME_LINE_QUESTION = 3; // 问卷随访类型
	static final Integer TIME_LINE_OTHER = 4; // 其他类型

	// 设备类型
	static final Integer CLIENT_IOS = 1;
	static final Integer CLIENT_ANDROID = 2;
	static final Integer CLIENT_WECHAT = 3;

	// 预约服务状态
	static final Integer STATE_ADD = 1; // 新增
	static final Integer STATE_HANDLED = 2; // 处理未做
	static final Integer STATE_OVER = 3; // 已做
	static final Integer STATE_ADD_CANCEL = 4; // 新增取消
	static final Integer STATE_HANDLED_CANCEL = 5; // 处理取消
	static final Integer STATE_HANDLED_PASS = 6; // 处理过期

	// 可见性
	static final Integer STATUS_ENABLE = 1; // 可见
	static final Integer STATUS_DISABLE = 0; // 不可见

	// 用户类型
	static final Integer USER_PATIENT = 100;   // 普通用户
	static final Integer USER_EMPLOYEE = 200;  // 医院员工
	static final Integer SIGN_PATIENT = 300;   // 签约用户
	static final Integer SIGN_EMPLOYEE = 400;  // 签约医生

	// 亲情账号
	static final Integer SELF = 0;   // 本人
	static final Integer FAMILY = 1;  // 家属

	// 付款状态
	static final Integer WAIT_PAY = 0;   // 等待付款
	static final Integer ALREADY_PAY = 1;   // 已付款
	static final Integer BACK_PAY = 2;   // 退款
	static final Integer PASS_PAY = 3;   // 过期

	// 科室介绍表中type字段
	static final Integer DEPT_ALL = 1; // 科室介绍和专家介绍中都显示
	static final Integer EXPERT_INTRODUCTION = 2; // 只在专家介绍中显示
	static final Integer DEPT_INTRODUCTION = 3; // 只在科室介绍中显示,院内版用户
	static final Integer DEPT_ENROLL = 4; // 继教报名中大科和单元
	static final Integer DEPT_ORDINARY = 5; // 排班中普通号里的科室
	static final Integer DEPT_EXPERT = 6; // 排班中专家号里的科室
	static final Integer DEPT_EXAM = 7; // 考试科室
	static final Integer DEPT_CONTACT = 8; // 通讯录科室
	// 挂号类型
	static final Integer ORDINARY_NO = 1; // 普通号
	static final Integer EXPERT_NO = 2; // 专家号

	// 默认头像
	static final String USER_AVATAR_M = "http://image.eeesys.com/default/user_m.png";      // 男性用户
	static final String USER_AVATAR_FM = "http://image.eeesys.com/default/user_fm.png";     // 女性用户
	static final String EMPLOYEE_AVATAR_M = "http://image.eeesys.com/default/doctor_m.png";  // 男性医生
	static final String EMPLOYEE_AVATAR_FM = "http://image.eeesys.com/default/doctor_fm.png"; // 女性医生
	static final String DEFAULT_AVATAR = "http://image.eeesys.com/default/default_avatar.png"; // 默认头像

	static final Integer FAILURE = 30000;
	static final Integer NEWS_NOT_FOUND = 30001;
	static final Integer DEVICE_CREATE_FAILED = 30002;
	static final Integer RESPONSE_FAILED = 30002;
	static final Integer DEPTS_NOT_FOUND = 30011;
	static final Integer LOGIN_FAILED = 30021; // 登录失败
	static final Integer NO_USER = 30022;  // 用户名不存在
	static final Integer WRONG_PASSWORD = 30023; // 密码不准确
	static final Integer MODUAL_EXISTS = 30024;//模块已存在
	static final Integer REGISTER_FAILED = 30030; // 注册失败
	static final Integer USER_EXITS = 30031; //用户已存在
	static final Integer USERNAME_ILLEGAL_LENGTH = 30032; // 用户名长度不合法
	static final Integer PASSWORD_ILLEGAL_LENGTH = 30033; // 密码长度不合法
	static final Integer IDCARD_ILLEGAL = 30034; // 身份证不合法
	static final Integer USERNAME_ILLEGAL = 30035; // 用户名不合法
	static final Integer PASSWORD_ILLEGAL = 30036; // 密码不合法
	static final Integer PASSWORD_DIFF = 30037; // 两次密码不一致
	static final Integer PHONE_ILLEGAL = 30038; // 手机号码不合法
	static final Integer NO_RESULT = 30041; // 没有相关数据
	static final Integer LENGTH_ILLEGAL = 30042;
	static final Integer ILLEGAL_REQUEST = 30051; // 非法请求
	static final Integer MOD_FAILED_NOUSER = 30061;
	static final Integer MOD_FAILED = 30071;
	static final Integer GROUP_DES_FAILED = 30072;
    static final Integer PATIENT_ADD_FAILED = 30073;
    static final Integer PATIENT_DEL_FAILED = 30074;
	static final Integer DELBIND_FAILED = 30075; // 除绑定失败
	static final Integer MEDEICALNO_ILLEGAL = 30076; // 医保号不能为空
	static final Integer HAVE_COMMITED = 30077; // 您已经提交，无需重复提交
	static final Integer NO_PATIENT = 30078;// 患者不存在
	static final Integer SEND_ERROR = 30079;// 发送错误
	static final Integer NO_NUMBER = 30080;// 号源不足
	static final Integer NO_ONLINE = 30081;// 病人处于离线状态稍后自动发送
	static final Integer DEPT_EXIST = 30082;// 科室已存在
	static final Integer HAVE_HANDLE = 30083; // 该病人当天已安排该类手术
	static final Integer NO_PERMISSOIN = 30084; // 没有权限
	static final Integer HAVE_ENABLED = 30085; // 已经被启用
	static final Integer HAVE_TIMEOUT = 30086; // 确认日期已过，无法确认
	static final Integer NO_CARDID = 30087; // 该用户没有对应的身份证号
	static final Integer NO_DATE = 30088; // 该月没有对应的工资信息
	static final Integer OVER_TIME = 30087; // 时间超出
	static final Integer FAILED_EXAM = 30088; // 考试不及格
	static final Integer ARGUMENT_ILLEGAL = 30089; // 参数不合法
	static final Integer PROFILE_EXISTED = 30090; // 该就诊人信息已存在
	static final Integer PROFILE_ENOUGH = 30091; // 不能添加超过5个就诊人
	static final Integer PROFILE_WARNING = 30092; // 不能删除自己的就诊信息
	static final Integer VIDEOCATE_HANE = 30093; // 视频类型已存在
	static final Integer LOGIN_WARNING = 30094; // 请核对您的姓名是否填写正确
	static final Integer PATIENT_INFO = 30095; // 请在个人信息中完善您的手机号、医保类型或医保卡号
	static final Integer DATA_EXITS = 30052;//数据记录已存在
	static final Integer DATA_COLLISION = 30053;//数据冲突
	static final Integer CLINIC_COLLISION = 30054;//门诊冲突
	static final Integer SIGN_EXISTS = 30096; // 您已签约该医生
	static final Integer SUBMIT_ERROR = 30100; // 审批失败，请稍后再试
	static final Integer BIND_ERROR = 30101; // 第三方登录失败，您未绑定账户
	static final Integer SOURCE_ILLEGAL = 30102; // 第三方来源不合法
	static final Integer UNIONID_ILLEGAL = 30103; // 第三方唯一标识不能为空
	static final Integer ADD_CLASS = 30104; // 请添加大分类
	static final Integer NOT_DEPT = 30105; // 请先选择科室
	static final Integer ADD_FAILED = 30106; // 数据添加失败
	// 返回结果描述
	static final Map<Integer, String> msgMap = new HashMap<Integer, String>();
	static{
		msgMap.put(FAILURE, "操作失败");
		msgMap.put(NEWS_NOT_FOUND, "新闻不存在");
		msgMap.put(DEVICE_CREATE_FAILED, "添加设备号失败");
		msgMap.put(RESPONSE_FAILED, "响应失败");
		msgMap.put(DEPTS_NOT_FOUND, "科室不存在");
		msgMap.put(LOGIN_FAILED, "用户名或密码错误");
		msgMap.put(REGISTER_FAILED, "注册失败");
		msgMap.put(NO_RESULT, "没有相关数据");
		msgMap.put(ILLEGAL_REQUEST, "非法请求");
		msgMap.put(MOD_FAILED_NOUSER, "修改失败，没有该用户");
		msgMap.put(MOD_FAILED, "修改失败，原始密码错误");
		msgMap.put(NO_USER, "用户不存在");
		msgMap.put(MODUAL_EXISTS,"模块已存在");
		msgMap.put(WRONG_PASSWORD, "原密码错误");
		msgMap.put(USER_EXITS, "用户已存在");
		msgMap.put(USERNAME_ILLEGAL_LENGTH, "用户名长度至少4个字符");
		msgMap.put(PASSWORD_ILLEGAL_LENGTH, "密码长度至少6个字符");
		msgMap.put(IDCARD_ILLEGAL, "身份证不合法");
		msgMap.put(USERNAME_ILLEGAL, "用户名必须以字母或下划线开头");
		msgMap.put(PASSWORD_ILLEGAL, "密码不合法");
		msgMap.put(PHONE_ILLEGAL, "手机号码不合法");
		msgMap.put(PASSWORD_DIFF, "两次密码不一致");
		msgMap.put(LENGTH_ILLEGAL, "输入的长度不合法");
		msgMap.put(GROUP_DES_FAILED, "该分组有患者不能删除");
		msgMap.put(PATIENT_ADD_FAILED,"该患者已存在于这个分组中");
		msgMap.put(PATIENT_DEL_FAILED,"该患者已经被删除");
		msgMap.put(DELBIND_FAILED, "解除绑定失败");
		msgMap.put(MEDEICALNO_ILLEGAL, "医保号不能为空");
		msgMap.put(HAVE_COMMITED, "您已经提交，无需重复提交");
		msgMap.put(NO_PATIENT, "该患者不存在,请确认手机号");
		msgMap.put(SEND_ERROR, "发送错误，请再次发送");
		msgMap.put(NO_NUMBER, "号源不足");
		msgMap.put(NO_ONLINE, "病人处于离线状态稍后自动发送");
		msgMap.put(DEPT_EXIST, "科室已存在");
		msgMap.put(HAVE_HANDLE, "该病人当天已安排该类手术");
		msgMap.put(NO_PERMISSOIN, "对不起，您目前没有此权限");
		msgMap.put(HAVE_ENABLED, "已有模板被启用,当前启用模板id为");
		msgMap.put(HAVE_TIMEOUT, "确认日期已过，无法确认");
		msgMap.put(NO_CARDID, "该用户没有对应的身份证号");
		msgMap.put(NO_DATE, "该月没有对应的工资信息");
		msgMap.put(OVER_TIME, "考试提交已超出时间");
		msgMap.put(FAILED_EXAM, "考试不通过需再次考试");
		msgMap.put(ARGUMENT_ILLEGAL, "参数不合法");
		msgMap.put(PROFILE_EXISTED, "该就诊人信息已存在");
		msgMap.put(PROFILE_WARNING, "您不能删除自己的就诊信息");
		msgMap.put(PROFILE_ENOUGH, "不能添加超过5个就诊人");
		msgMap.put(VIDEOCATE_HANE, "视频类型已存在");
		msgMap.put(LOGIN_WARNING, "请核对您的姓名是否填写正确");
		msgMap.put(PATIENT_INFO, "请在个人信息中完善您的手机号、医保类型等信息");
		msgMap.put(DATA_EXITS, "该条数据记录已存在");
		msgMap.put(DATA_COLLISION, "数据冲突");
		msgMap.put(CLINIC_COLLISION, "门诊冲突");
		msgMap.put(SIGN_EXISTS, "您已签约该医生");
		msgMap.put(SUBMIT_ERROR, "审批失败，请稍后再试");
		msgMap.put(BIND_ERROR, "第三方登录失败，您未绑定账户");
		msgMap.put(SOURCE_ILLEGAL, "第三方来源不合法");
		msgMap.put(UNIONID_ILLEGAL, "第三方唯一标识不能为空");
		msgMap.put(ADD_CLASS, "请添加大分类");
		msgMap.put(NOT_DEPT, "请先选择科室");
		msgMap.put(ADD_FAILED, "数据添加失败");
	}

	// 设置接口返回信息和错误描述
	void setErrcode(Message msg, Integer errcode) {
		String errmsg = Tools.getDesc(msgMap.get(errcode), "未知错误");
		msg.setErrcode(errcode);
		msg.setErrmsg(errmsg);
	}

	// 发送邮件
	void mail(String title, String content) {
		String url = "http://cloud.eeesys.com/mail/send.php";
		Map<String, String> param = new HashMap<String, String>();
		param.put("to", "yangh@eeesys.com");
		param.put("title", title);
		param.put("content", content);
		String str = HttpUtil.post(url, param);
	}

	// 抓取数据出问题时发邮件
	void mail(String title) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String now = sdf.format(new java.util.Date());
		mail(now + ":" + title, "");
	}

	// 替换html中特殊字符
	String replaceHtml(String content) {
		if (content==null) return "";
		String html = content;
		html = html.replaceAll( "\"", "&quot;");
		html = html.replaceAll("\t","").replaceAll("\n","").replaceAll("\r","");
		html = html.replaceAll("<v:imagedata[^>]*?>", "</v:imagedata>");
		return html;
	}
	
		// 获取数据库连接 add by liuzq on 20180522
	public Connection getConn() { 
		String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
		String userName = "user"; 
		String passwrod = "123456"; 
		String url = "jdbc:sqlserver://192.168.2.72:1433;databaseName=THIS4"; 
		Connection conn = null; 
		try { 
			Class.forName(driver); 
			conn = DriverManager.getConnection(url, userName, passwrod); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
		return conn; 
	}
	
	public  static String getStrFromObject(Object o,String def) {
		if(o==null) return def;
		return o.toString();
	}

	
%>
<%
    Input input = new Input(request); // 请求对象

	Integer hospital = input.getInt("hospital", -1); // 医院id
    String act = input.getDesc("act", "list"); // 访问类型

	// 生成log
	String path = Tools.getDesc(input.getPath(), "");
	if (path.startsWith("/v2/")) {
		String useragent = Tools.getDesc(input.getUseragent(), "");
		String params = Tools.getDesc(input.getParams(), "");
		String ip = input.getIp();
		String[] p = params.split("&");
		String appType = "";
		String hospitalStr = "";
		for (int i = 0; i < p.length; i++) {
			String value = p[i];
			if (value.startsWith("hospital")) {
				hospitalStr = value.replace("hospital=", "");
			}
			if (value.startsWith("app_type")) {
				appType = value.replace("app_type=", "");
			}
		}

		HashMap logMap = new HashMap();
		logMap.put("ipv4", ip);
		logMap.put("hospital", hospitalStr);
		logMap.put("app_type", appType);
		logMap.put("create_time", System.currentTimeMillis());
		logMap.put("useragent", useragent);
		logMap.put("params", params);
		logMap.put("url", path);
		DBTools.db("basedb").addItem("sys_log", logMap);
	}
	
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
	
%>
